package de.uni.muenster.idw2.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import de.uni.muenster.idw2.data.Building;
import de.uni.muenster.idw2.data.Connection;
import de.uni.muenster.idw2.data.Floor;
import de.uni.muenster.idw2.data.Node;
import de.uni.muenster.idw2.data.Position;
import de.uni.muenster.idw2.data.Room;


public class OFMXMLHandler extends DefaultHandler{

	// ===========================================================
	// Fields
	// ===========================================================
	
	// states
	private boolean inBuilding = false;
	private boolean inFloor = false;
	private boolean inRoom = false;
	private boolean inInnerRoom = false;
	private boolean inObject = false;
	
	
	private Building building;
	private Floor currFloor;
	private Room currRoom;
	
	private ArrayList<Connection> connections = new ArrayList<Connection>();

	

	// ===========================================================
	// Getter & Setter
	// ===========================================================


	public Building getBuilding() {
		return building;
	}
	
	// ===========================================================
	// Methods
	// ===========================================================
	

	/*
	 * runs throw the list of connections, merges the the two with the given ids and gives them to the according rooms
	 */
	private void mergeConnections(int id1, int id2){

		Connection con1 = null;
		for (Connection c : connections){
			if (c.getId1()==id1){
				con1 = c;
				break;
			}
		}
		if (con1 != null){
			connections.remove(con1);
			for (Connection c : connections){
				if (c.getId1()==id2){
					Connection newCon = new Connection(con1, c);
					newCon.getRoom1().addConnection(newCon);
					newCon.getRoom2().addConnection(newCon);
					break;
				}
			}
		}
		
	}
	
	
	@Override
	public void startDocument() throws SAXException {
	
	}

	

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/** Gets be called on opening tags like: 
	 * <tag> 
	 * Can provide attribute(s), when xml was like:
	 * <tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {


		if (inRoom){

			if (inInnerRoom){
				if (localName.equals("node")){
					currRoom.addInnerNode(new Node(Double.parseDouble(atts.getValue("x")), Double.parseDouble(atts.getValue("y")), currFloor.getNumber(), Integer.parseInt(atts.getValue("h"))));
				}	
			}
			
			else if (localName.equals("node")){

				currRoom.addNode(new Node(Double.parseDouble(atts.getValue("x")), Double.parseDouble(atts.getValue("y")), currFloor.getNumber(), Integer.parseInt(atts.getValue("h"))));

			}
			
			else if (localName.equals("innerRoom")){
				inInnerRoom = true;
			}
			else if (localName.equals("object")){
				currRoom.addObject(new de.uni.muenster.idw2.data.Landmark(atts.getValue("name"), new Position(Double.parseDouble(atts.getValue("x")), Double.parseDouble(atts.getValue("y")), currFloor.getNumber())));
			}
			else if (localName.equals("connection")){

				connections.add(new Connection(currRoom, new Position(Double.parseDouble(atts.getValue("x")), Double.parseDouble(atts.getValue("y")), currFloor.getNumber()), Integer.parseInt(atts.getValue("id"))));
			}
			else if (localName.equals("tag")){
				if (atts.getValue("k").equals("description")) // atts.getValue("k")!= null && 
				currRoom.setDescription(atts.getValue("v"));
			}
			
		}
		else if (localName.equals("building")){
			inBuilding = true;
			building = Building.getBuilding();
			building.resetBuilding();
			building = Building.getBuilding();
			building.setId(Double.parseDouble(atts.getValue("id")));
			building.setName(atts.getValue("name"));
			building.setGeopath(atts.getValue("geopath"));
		}
		else if (localName.equals("floor")){
			inFloor = true;
			currFloor = new Floor (Double.parseDouble(atts.getValue("id")), Integer.parseInt(atts.getValue("number")));
		}
		else if (localName.equals("room")){
			inRoom = true;
			currRoom = new Room(Double.parseDouble(atts.getValue("id")),atts.getValue("name"));
		}
		else if (localName.equals("connections")){
			mergeConnections(Integer.parseInt(atts.getValue("connectionid1")), Integer.parseInt(atts.getValue("connectionid2")));
		}
		else if (localName.equals("tag")){
			if ( atts.getValue("k").equals("description")) //atts.getValue("k")!= null &&
			building.setDescription(atts.getValue("v"));
			else if (atts.getValue("k").equals("image")) // atts.getValue("k")!= null && 
				building.addImage(atts.getValue("v"));
		}
		
	}
	
	/** Gets be called on closing tags like: 
	 * </tag> */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		
		if (localName.equals("building")){
			inBuilding = false;
			// do nothing for the moment...
		}
		else if (localName.equals("floor")){
			inFloor = false;
			building.addFloor(currFloor);
		}
		else if (localName.equals("room")){
			inRoom = false;
			currFloor.addRoom(currRoom);
		}
		else if (localName.equals("innerRoom")){
			inInnerRoom = false;
		}
		
	}
	
	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */
	@Override
    public void characters(char ch[], int start, int length) {
	
    }
}

